home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / function.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-16  |  39.2 KB  |  1,620 lines

  1. /* Variable function expansion for GNU Make.
  2. Copyright (C) 1988,89,91,92,93,94,95,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "filedef.h"
  21. #include "variable.h"
  22. #include "dep.h"
  23. #include "job.h"
  24. #include "commands.h"
  25.  
  26. #ifdef _AMIGA
  27. #include "amiga.h"
  28. #endif
  29. #ifdef WINDOWS32
  30. #include <windows.h>
  31. #include <io.h>
  32. #include "sub_proc.h"
  33. #endif
  34.  
  35. static char *string_glob PARAMS ((char *line));
  36.  
  37. /* Store into VARIABLE_BUFFER at O the result of scanning TEXT and replacing
  38.    each occurrence of SUBST with REPLACE. TEXT is null-terminated.  SLEN is
  39.    the length of SUBST and RLEN is the length of REPLACE.  If BY_WORD is
  40.    nonzero, substitutions are done only on matches which are complete
  41.    whitespace-delimited words.  If SUFFIX_ONLY is nonzero, substitutions are
  42.    done only at the ends of whitespace-delimited words.  */
  43.  
  44. char *
  45. subst_expand (o, text, subst, replace, slen, rlen, by_word, suffix_only)
  46.      char *o;
  47.      char *text;
  48.      char *subst, *replace;
  49.      unsigned int slen, rlen;
  50.      int by_word, suffix_only;
  51. {
  52.   register char *t = text;
  53.   register char *p;
  54.  
  55.   if (slen == 0 && !by_word && !suffix_only)
  56.     {
  57.       /* The first occurrence of "" in any string is its end.  */
  58.       o = variable_buffer_output (o, t, strlen (t));
  59.       if (rlen > 0)
  60.     o = variable_buffer_output (o, replace, rlen);
  61.       return o;
  62.     }
  63.  
  64.   do
  65.     {
  66.       if ((by_word | suffix_only) && slen == 0)
  67.     /* When matching by words, the empty string should match
  68.        the end of each word, rather than the end of the whole text.  */
  69.     p = end_of_token (next_token (t));
  70.       else
  71.     {
  72.       p = sindex (t, 0, subst, slen);
  73.       if (p == 0)
  74.         {
  75.           /* No more matches.  Output everything left on the end.  */
  76.           o = variable_buffer_output (o, t, strlen (t));
  77.           return o;
  78.         }
  79.     }
  80.  
  81.       /* Output everything before this occurrence of the string to replace.  */
  82.       if (p > t)
  83.     o = variable_buffer_output (o, t, p - t);
  84.  
  85.       /* If we're substituting only by fully matched words,
  86.      or only at the ends of words, check that this case qualifies.  */
  87.       if ((by_word
  88.        && ((p > t && !isblank (p[-1]))
  89.            || (p[slen] != '\0' && !isblank (p[slen]))))
  90.       || (suffix_only
  91.           && (p[slen] != '\0' && !isblank (p[slen]))))
  92.     /* Struck out.  Output the rest of the string that is
  93.        no longer to be replaced.  */
  94.     o = variable_buffer_output (o, subst, slen);
  95.       else if (rlen > 0)
  96.     /* Output the replacement string.  */
  97.     o = variable_buffer_output (o, replace, rlen);
  98.  
  99.       /* Advance T past the string to be replaced.  */
  100.       t = p + slen;
  101.     } while (*t != '\0');
  102.  
  103.   return o;
  104. }
  105.  
  106.  
  107. /* Store into VARIABLE_BUFFER at O the result of scanning TEXT
  108.    and replacing strings matching PATTERN with REPLACE.
  109.    If PATTERN_PERCENT is not nil, PATTERN has already been
  110.    run through find_percent, and PATTERN_PERCENT is the result.
  111.    If REPLACE_PERCENT is not nil, REPLACE has already been
  112.    run through find_percent, and REPLACE_PERCENT is the result.  */
  113.  
  114. char *
  115. patsubst_expand (o, text, pattern, replace, pattern_percent, replace_percent)
  116.      char *o;
  117.      char *text;
  118.      register char *pattern, *replace;
  119.      register char *pattern_percent, *replace_percent;
  120. {
  121.   unsigned int pattern_prepercent_len, pattern_postpercent_len;
  122.   unsigned int replace_prepercent_len, replace_postpercent_len;
  123.   char *t;
  124.   unsigned int len;
  125.   int doneany = 0;
  126.  
  127.   /* We call find_percent on REPLACE before checking PATTERN so that REPLACE
  128.      will be collapsed before we call subst_expand if PATTERN has no %.  */
  129.   if (replace_percent == 0)
  130.     replace_percent = find_percent (replace);
  131.   if (replace_percent != 0)
  132.     {
  133.       /* Record the length of REPLACE before and after the % so
  134.      we don't have to compute these lengths more than once.  */
  135.       replace_prepercent_len = replace_percent - replace;
  136.       replace_postpercent_len = strlen (replace_percent + 1);
  137.     }
  138.   else
  139.     /* We store the length of the replacement
  140.        so we only need to compute it once.  */
  141.     replace_prepercent_len = strlen (replace);
  142.  
  143.   if (pattern_percent == 0)
  144.     pattern_percent = find_percent (pattern);
  145.   if (pattern_percent == 0)
  146.     /* With no % in the pattern, this is just a simple substitution.  */
  147.     return subst_expand (o, text, pattern, replace,
  148.              strlen (pattern), strlen (replace), 1, 0);
  149.  
  150.   /* Record the length of PATTERN before and after the %
  151.      so we don't have to compute it more than once.  */
  152.   pattern_prepercent_len = pattern_percent - pattern;
  153.   pattern_postpercent_len = strlen (pattern_percent + 1);
  154.  
  155.   while ((t = find_next_token (&text, &len)) != 0)
  156.     {
  157.       int fail = 0;
  158.  
  159.       /* Is it big enough to match?  */
  160.       if (len < pattern_prepercent_len + pattern_postpercent_len)
  161.     fail = 1;
  162.  
  163.       /* Does the prefix match? */
  164.       if (!fail && pattern_prepercent_len > 0
  165.       && (*t != *pattern
  166.           || t[pattern_prepercent_len - 1] != pattern_percent[-1]
  167.           || strncmp (t + 1, pattern + 1, pattern_prepercent_len - 1)))
  168.     fail = 1;
  169.  
  170.       /* Does the suffix match? */
  171.       if (!fail && pattern_postpercent_len > 0
  172.       && (t[len - 1] != pattern_percent[pattern_postpercent_len]
  173.           || t[len - pattern_postpercent_len] != pattern_percent[1]
  174.           || strncmp (&t[len - pattern_postpercent_len],
  175.               &pattern_percent[1], pattern_postpercent_len - 1)))
  176.     fail = 1;
  177.  
  178.       if (fail)
  179.     /* It didn't match.  Output the string.  */
  180.     o = variable_buffer_output (o, t, len);
  181.       else
  182.     {
  183.       /* It matched.  Output the replacement.  */
  184.  
  185.       /* Output the part of the replacement before the %.  */
  186.       o = variable_buffer_output (o, replace, replace_prepercent_len);
  187.  
  188.       if (replace_percent != 0)
  189.         {
  190.           /* Output the part of the matched string that
  191.          matched the % in the pattern.  */
  192.           o = variable_buffer_output (o, t + pattern_prepercent_len,
  193.                       len - (pattern_prepercent_len
  194.                          + pattern_postpercent_len));
  195.           /* Output the part of the replacement after the %.  */
  196.           o = variable_buffer_output (o, replace_percent + 1,
  197.                       replace_postpercent_len);
  198.         }
  199.     }
  200.  
  201.       /* Output a space, but not if the replacement is "".  */
  202.       if (fail || replace_prepercent_len > 0
  203.       || (replace_percent != 0 && len + replace_postpercent_len > 0))
  204.     {
  205.       o = variable_buffer_output (o, " ", 1);
  206.       doneany = 1;
  207.     }
  208.     }
  209.   if (doneany)
  210.     /* Kill the last space.  */
  211.     --o;
  212.  
  213.   return o;
  214. }
  215.  
  216. /* Handle variable-expansion-time functions such as $(dir foo/bar) ==> foo/  */
  217.  
  218. /* These enumeration constants distinguish the
  219.    various expansion-time built-in functions.  */
  220.  
  221. enum function
  222.   {
  223.     function_subst,
  224.     function_addsuffix,
  225.     function_addprefix,
  226.     function_dir,
  227.     function_notdir,
  228.     function_suffix,
  229.     function_basename,
  230.     function_wildcard,
  231.     function_firstword,
  232.     function_word,
  233.     function_words,
  234.     function_wordlist,
  235.     function_findstring,
  236.     function_strip,
  237.     function_join,
  238.     function_patsubst,
  239.     function_filter,
  240.     function_filter_out,
  241.     function_foreach,
  242.     function_sort,
  243.     function_origin,
  244.     function_shell,
  245.     function_invalid
  246.   };
  247.  
  248. /* Greater than the length of any function name.  */
  249. #define MAXFUNCTIONLEN 11
  250.  
  251. /* The function names and lengths of names, for looking them up.  */
  252.  
  253. static struct
  254.   {
  255.     char *name;
  256.     unsigned int len;
  257.     enum function function;
  258.   } function_table[] =
  259.   {
  260.     { "subst", 5, function_subst },
  261.     { "addsuffix", 9, function_addsuffix },
  262.     { "addprefix", 9, function_addprefix },
  263.     { "dir", 3, function_dir },
  264.     { "notdir", 6, function_notdir },
  265.     { "suffix", 6, function_suffix },
  266.     { "basename", 8, function_basename },
  267.     { "wildcard", 8, function_wildcard },
  268.     { "firstword", 9, function_firstword },
  269.     { "word", 4, function_word },
  270.     { "words", 5, function_words },
  271.     { "wordlist", 8, function_wordlist },
  272.     { "findstring", 10, function_findstring },
  273.     { "strip", 5, function_strip },
  274.     { "join", 4, function_join },
  275.     { "patsubst", 8, function_patsubst },
  276.     { "filter", 6, function_filter },
  277.     { "filter-out", 10, function_filter_out },
  278.     { "foreach", 7, function_foreach },
  279.     { "sort", 4, function_sort },
  280.     { "origin", 6, function_origin },
  281.     { "shell", 5, function_shell },
  282.     { 0, 0, function_invalid }
  283.   };
  284.  
  285. /* Return 1 if PATTERN matches WORD, 0 if not.  */
  286.  
  287. int
  288. pattern_matches (pattern, percent, word)
  289.      register char *pattern, *percent, *word;
  290. {
  291.   unsigned int sfxlen, wordlen;
  292.  
  293.   if (percent == 0)
  294.     {
  295.       unsigned int len = strlen (pattern) + 1;
  296.       char *new = (char *) alloca (len);
  297.       bcopy (pattern, new, len);
  298.       pattern = new;
  299.       percent = find_percent (pattern);
  300.       if (percent == 0)
  301.     return streq (pattern, word);
  302.     }
  303.  
  304.   sfxlen = strlen (percent + 1);
  305.   wordlen = strlen (word);
  306.  
  307.   if (wordlen < (percent - pattern) + sfxlen
  308.       || strncmp (pattern, word, percent - pattern))
  309.     return 0;
  310.  
  311.   return !strcmp (percent + 1, word + (wordlen - sfxlen));
  312. }
  313.  
  314. int shell_function_pid = 0, shell_function_completed;
  315.  
  316. /* Perform the function specified by FUNCTION on the text at TEXT.
  317.    END is points to the end of the argument text (exclusive).
  318.    The output is written into VARIABLE_BUFFER starting at O.  */
  319.  
  320. /* Note this absorbs a semicolon and is safe to use in conditionals.  */
  321. #define BADARGS(func)                                                         \
  322.   if (reading_filename != 0)                                                  \
  323.     makefile_fatal (reading_filename, *reading_lineno_ptr,                    \
  324.             "insufficient arguments to function `%s'",                \
  325.             func);                                                    \
  326.   else                                                                        \
  327.     fatal ("insufficient arguments to function `%s'", func)
  328.  
  329. static char *
  330. expand_function (o, function, text, end)
  331.      char *o;
  332.      enum function function;
  333.      char *text;
  334.      char *end;
  335. {
  336.   char *p, *p2, *p3;
  337.   unsigned int i, j, len;
  338.   int doneany = 0;
  339.   int count;
  340.   char endparen = *end, startparen = *end == ')' ? '(' : '{';
  341.  
  342.   switch (function)
  343.     {
  344.     default:
  345.       abort ();
  346.       break;
  347.  
  348. #ifndef VMS /* not supported for vms yet */
  349.     case function_shell:
  350.       {
  351. #ifdef WINDOWS32
  352.     SECURITY_ATTRIBUTES saAttr;
  353.     HANDLE hIn;
  354.     HANDLE hErr;
  355.     HANDLE hChildOutRd;
  356.     HANDLE hChildOutWr;
  357.     HANDLE hProcess;
  358. #endif
  359. #ifdef __MSDOS__
  360.     FILE *fpipe;
  361. #endif
  362.     char **argv;
  363.     char *error_prefix;
  364. #ifndef _AMIGA
  365.     char **envp;
  366.     int pipedes[2];
  367.     int pid;
  368. #endif
  369.  
  370.     /* Expand the command line.  */
  371.     text = expand_argument (text, end);
  372.  
  373. #ifndef __MSDOS__
  374.     /* Construct the argument list.  */
  375.     argv = construct_command_argv (text,
  376.                        (char **) NULL, (struct file *) 0);
  377.     if (argv == 0)
  378.       break;
  379. #endif
  380.  
  381. #ifndef _AMIGA
  382.     /* Using a target environment for `shell' loses in cases like:
  383.         export var = $(shell echo foobie)
  384.        because target_environment hits a loop trying to expand $(var)
  385.        to put it in the environment.  This is even more confusing when
  386.        var was not explicitly exported, but just appeared in the
  387.        calling environment.  */
  388. #if 1
  389.     envp = environ;
  390. #else
  391.     /* Construct the environment.  */
  392.     envp = target_environment ((struct file *) 0);
  393. #endif
  394. #endif  /* Not Amiga.  */
  395.  
  396.     /* For error messages.  */
  397.     if (reading_filename != 0)
  398.       {
  399.         error_prefix = (char *) alloca (strlen (reading_filename) + 100);
  400.         sprintf (error_prefix,
  401.              "%s:%u: ", reading_filename, *reading_lineno_ptr);
  402.       }
  403.     else
  404.       error_prefix = "";
  405.  
  406. #ifndef _AMIGA
  407. # ifdef WINDOWS32
  408.     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  409.     saAttr.bInheritHandle = TRUE;
  410.     saAttr.lpSecurityDescriptor = NULL;
  411.  
  412.     if (DuplicateHandle(GetCurrentProcess(),
  413.                 GetStdHandle(STD_INPUT_HANDLE),
  414.                 GetCurrentProcess(),
  415.                 &hIn,
  416.                 0,
  417.                 TRUE,
  418.                 DUPLICATE_SAME_ACCESS) == FALSE) {
  419.       fatal("create_child_process: DuplicateHandle(In) failed (e=%d)\n",
  420.         GetLastError());
  421.     }
  422.     if (DuplicateHandle(GetCurrentProcess(),
  423.                 GetStdHandle(STD_ERROR_HANDLE),
  424.                 GetCurrentProcess(),
  425.                 &hErr,
  426.                 0,
  427.                 TRUE,
  428.                 DUPLICATE_SAME_ACCESS) == FALSE) {
  429.       fatal("create_child_process: DuplicateHandle(Err) failed (e=%d)\n",
  430.         GetLastError());
  431.     }
  432.  
  433.     if (!CreatePipe(&hChildOutRd, &hChildOutWr, &saAttr, 0))
  434.       fatal("CreatePipe() failed (e=%d)\n", GetLastError());
  435.  
  436.     hProcess = process_init_fd(hIn, hChildOutWr, hErr);
  437.  
  438.     if (!hProcess)
  439.       fatal("expand_function: process_init_fd() failed\n");
  440.     else
  441.       process_register(hProcess);
  442.  
  443.     /* make sure that CreateProcess() has Path it needs */
  444.     sync_Path_environment();
  445.  
  446.     if (!process_begin(hProcess, argv, envp, argv[0], NULL))
  447.         pid = (int) hProcess;
  448.     else
  449.         fatal("expand_function: unable to launch process (e=%d)\n",
  450.               process_last_err(hProcess));
  451.  
  452.     /* set up to read data from child */
  453.     pipedes[0] = _open_osfhandle((long) hChildOutRd, O_RDONLY);
  454.  
  455.     /* this will be closed almost right away */
  456.     pipedes[1] = _open_osfhandle((long) hChildOutWr, O_APPEND);
  457. # else /* WINDOWS32 */
  458. #  ifdef __MSDOS__
  459.     {
  460.       /* MSDOS can't fork, but it has `popen'.
  461.          (Bwt, why isn't `popen' used in all the versions?) */
  462.       struct variable *sh = lookup_variable ("SHELL", 5);
  463.       int e;
  464.       extern int dos_command_running, dos_status;
  465.  
  466.       /* Make sure not to bother processing an empty line.  */
  467.       while (isblank (*text))
  468.         ++text;
  469.       if (*text == '\0')
  470.         break;
  471.  
  472.       if (sh)
  473.         {
  474.           char buf[PATH_MAX + 7];
  475.           /* This makes sure $SHELL value is used by $(shell), even
  476.          though the target environment is not passed to it.  */
  477.           sprintf (buf, "SHELL=%s", sh->value);
  478.           putenv (buf);
  479.         }
  480.  
  481.       e = errno;
  482.       errno = 0;
  483.       dos_command_running = 1;
  484.       dos_status = 0;
  485.       fpipe = popen (text, "rt");
  486.       dos_command_running = 0;
  487.       if (!fpipe || dos_status)
  488.         {
  489.           pipedes[0] = -1;
  490.           pid = -1;
  491.           if (dos_status)
  492.         errno = EINTR;
  493.           else if (errno == 0)
  494.         errno = ENOMEM;
  495.           shell_function_completed = -1;
  496.         }
  497.       else
  498.         {
  499.           pipedes[0] = fileno (fpipe);
  500.           pid = 42;
  501.           errno = e;
  502.           shell_function_completed = 1;
  503.         }
  504.     }
  505.     if (pipedes[0] < 0)
  506. #  else /* ! __MSDOS__ */
  507.     if (pipe (pipedes) < 0)
  508. #  endif /* __MSDOS__ */
  509.       {
  510.         perror_with_name (error_prefix, "pipe");
  511.         break;
  512.       }
  513.  
  514. #  ifndef  __MSDOS__
  515.     pid = vfork ();
  516.     if (pid < 0)
  517.       perror_with_name (error_prefix, "fork");
  518.     else if (pid == 0)
  519.       child_execute_job (0, pipedes[1], argv, envp);
  520.     else
  521. #  endif /* ! __MSDOS__ */
  522. # endif /* WINDOWS32 */
  523.       {
  524.         /* We are the parent.  */
  525.  
  526.         char *buffer;
  527.         unsigned int maxlen;
  528.         int cc;
  529. #if 0
  530.         for (i = 0; envp[i] != 0; ++i)
  531.           free (envp[i]);
  532.         free ((char *) envp);
  533. #endif
  534.  
  535.         /* Record the PID for reap_children.  */
  536.         shell_function_pid = pid;
  537. #ifndef  __MSDOS__
  538.         shell_function_completed = 0;
  539.  
  540.         /* Free the storage only the child needed.  */
  541.         free (argv[0]);
  542.         free ((char *) argv);
  543.  
  544.         /* Close the write side of the pipe.  */
  545.         (void) close (pipedes[1]);
  546. #endif
  547.  
  548.         /* Set up and read from the pipe.  */
  549.  
  550.         maxlen = 200;
  551.         buffer = (char *) xmalloc (maxlen + 1);
  552.  
  553.         /* Read from the pipe until it gets EOF.  */
  554.         i = 0;
  555.         do
  556.           {
  557.         if (i == maxlen)
  558.           {
  559.             maxlen += 512;
  560.             buffer = (char *) xrealloc (buffer, maxlen + 1);
  561.           }
  562.  
  563.         errno = 0;
  564.         cc = read (pipedes[0], &buffer[i], maxlen - i);
  565.         if (cc > 0)
  566.           i += cc;
  567.           }
  568. #ifdef EINTR
  569.         while (cc > 0 || errno == EINTR);
  570. #else
  571.         while (cc > 0);
  572. #endif
  573.  
  574.         /* Close the read side of the pipe.  */
  575. #ifdef  __MSDOS__
  576.         if (fpipe)
  577.           (void) pclose (fpipe);
  578. #else
  579.         (void) close (pipedes[0]);
  580. #endif
  581.  
  582.         /* Loop until child_handler sets shell_function_completed
  583.            to the status of our child shell.  */
  584.         while (shell_function_completed == 0)
  585.           reap_children (1, 0);
  586.  
  587.         shell_function_pid = 0;
  588.  
  589.         /* The child_handler function will set shell_function_completed
  590.            to 1 when the child dies normally, or to -1 if it
  591.            dies with status 127, which is most likely an exec fail.  */
  592.  
  593.         if (shell_function_completed == -1)
  594.           {
  595.         /* This most likely means that the execvp failed,
  596.            so we should just write out the error message
  597.            that came in over the pipe from the child.  */
  598.         fputs (buffer, stderr);
  599.         fflush (stderr);
  600.           }
  601.         else
  602.           {
  603.         /* The child finished normally.  Replace all
  604.            newlines in its output with spaces, and put
  605.            that in the variable output buffer.  */
  606.         if (i > 0)
  607.           {
  608.             if (buffer[i - 1] == '\n')
  609.               {
  610.             if (i > 1 && buffer[i - 2] == '\r')
  611.               --i;
  612.             buffer[--i] = '\0';
  613.               }
  614.             else
  615.               buffer[i] = '\0';
  616.  
  617.             p = buffer;
  618.             for (p2=p; *p != '\0'; ++p)
  619.               {
  620.             if (p[0] == '\r' && p[1] == '\n')
  621.               continue;
  622.             if (*p == '\n')
  623.               *p2++ = ' ';
  624.             else
  625.               *p2++ = *p;
  626.               }
  627.             *p2 = '\0';
  628.             o = variable_buffer_output (o, buffer, i);
  629.           }
  630.           }
  631.  
  632.         free (buffer);
  633.       }
  634. #else   /* Amiga */
  635.      {
  636.        /* Amiga can't fork nor spawn, but I can start a program with
  637.           redirection of my choice.  However, this means that we
  638.           don't have an opportunity to reopen stdout to trap it.  Thus,
  639.           we save our own stdout onto a new descriptor and dup a temp
  640.           file's descriptor onto our stdout temporarily.  After we
  641.           spawn the shell program, we dup our own stdout back to the
  642.           stdout descriptor.  The buffer reading is the same as above,
  643.           except that we're now reading from a file.  */
  644. #include <dos/dos.h>
  645. #include <proto/dos.h>
  646.  
  647.        BPTR child_stdout;
  648.        char tmp_output[FILENAME_MAX];
  649.        unsigned int maxlen = 200;
  650.        int cc;
  651.        char * buffer, * ptr;
  652.        char ** aptr;
  653.        int len = 0;
  654.  
  655.        strcpy (tmp_output, "t:MakeshXXXXXXXX");
  656.        mktemp (tmp_output);
  657.        child_stdout = Open (tmp_output, MODE_NEWFILE);
  658.  
  659.        for (aptr=argv; *aptr; aptr++)
  660.          {
  661.            len += strlen (*aptr) + 1;
  662.          }
  663.  
  664.        buffer = xmalloc (len + 1);
  665.        ptr = buffer;
  666.  
  667.        for (aptr=argv; *aptr; aptr++)
  668.          {
  669.            strcpy (ptr, *aptr);
  670.            ptr += strlen (ptr) + 1;
  671.            *ptr ++ = ' ';
  672.            *ptr = 0;
  673.          }
  674.  
  675.        ptr[-1] = '\n';
  676.  
  677.        Execute (buffer, NULL, child_stdout);
  678.        free (buffer);
  679.  
  680.        Close (child_stdout);
  681.  
  682.        child_stdout = Open (tmp_output, MODE_OLDFILE);
  683.  
  684.        buffer = xmalloc (maxlen);
  685.        i = 0;
  686.        do
  687.          {
  688.            if (i == maxlen)
  689.          {
  690.            maxlen += 512;
  691.            buffer = (char *) xrealloc (buffer, maxlen + 1);
  692.          }
  693.  
  694.            cc = Read (child_stdout, &buffer[i], maxlen - i);
  695.            if (cc > 0)
  696.          i += cc;
  697.          } while (cc > 0);
  698.  
  699.        Close (child_stdout);
  700.        DeleteFile (tmp_output);
  701.  
  702.        if (i > 0)
  703.          {
  704.            if (buffer[i - 1] == '\n')
  705.          buffer[--i] = '\0';
  706.            else
  707.          buffer[i] = '\0';
  708.            p = buffer;
  709.            while ((p = index (p, '\n')) != 0)
  710.          *p++ = ' ';
  711.            o = variable_buffer_output (o, buffer, i);
  712.          }
  713.        free (buffer);
  714.      }
  715. #endif  /* Not Amiga.  */
  716.  
  717.     free (text);
  718.     break;
  719.       }
  720. #endif /* !VMS */
  721.  
  722.     case function_origin:
  723.       /* Expand the argument.  */
  724.       text = expand_argument (text, end);
  725.  
  726.       {
  727.     register struct variable *v = lookup_variable (text, strlen (text));
  728.     if (v == 0)
  729.       o = variable_buffer_output (o, "undefined", 9);
  730.     else
  731.       switch (v->origin)
  732.         {
  733.         default:
  734.         case o_invalid:
  735.           abort ();
  736.           break;
  737.         case o_default:
  738.           o = variable_buffer_output (o, "default", 7);
  739.           break;
  740.         case o_env:
  741.           o = variable_buffer_output (o, "environment", 11);
  742.           break;
  743.         case o_file:
  744.           o = variable_buffer_output (o, "file", 4);
  745.           break;
  746.         case o_env_override:
  747.           o = variable_buffer_output (o, "environment override", 20);
  748.           break;
  749.         case o_command:
  750.           o = variable_buffer_output (o, "command line", 12);
  751.           break;
  752.         case o_override:
  753.           o = variable_buffer_output (o, "override", 8);
  754.           break;
  755.         case o_automatic:
  756.           o = variable_buffer_output (o, "automatic", 9);
  757.           break;
  758.         }
  759.       }
  760.  
  761.       free (text);
  762.       break;
  763.  
  764.     case function_sort:
  765.       /* Expand the argument.  */
  766.       text = expand_argument (text, end);
  767.  
  768.       {
  769.     char **words = (char **) xmalloc (10 * sizeof (char *));
  770.     unsigned int nwords = 10;
  771.     register unsigned int wordi = 0;
  772.     char *t;
  773.  
  774.     /* Chop TEXT into words and put them in WORDS.  */
  775.     t = text;
  776.     while ((p = find_next_token (&t, &len)) != 0)
  777.       {
  778.         if (wordi >= nwords - 1)
  779.           {
  780.         nwords *= 2;
  781.         words = (char **) xrealloc ((char *) words,
  782.                         nwords * sizeof (char *));
  783.           }
  784.         words[wordi++] = savestring (p, len);
  785.       }
  786.  
  787.     if (wordi > 0)
  788.       {
  789.         /* Now sort the list of words.  */
  790.         qsort ((char *) words, wordi, sizeof (char *), alpha_compare);
  791.  
  792.         /* Now write the sorted list.  */
  793.         for (i = 0; i < wordi; ++i)
  794.           {
  795.         len = strlen (words[i]);
  796.         if (i == wordi - 1 || strlen (words[i + 1]) != len
  797.             || strcmp (words[i], words[i + 1]))
  798.           {
  799.             o = variable_buffer_output (o, words[i], len);
  800.             o = variable_buffer_output (o, " ", 1);
  801.           }
  802.         free (words[i]);
  803.           }
  804.         /* Kill the last space.  */
  805.         --o;
  806.       }
  807.  
  808.     free ((char *) words);
  809.       }
  810.  
  811.       free (text);
  812.       break;
  813.  
  814.     case function_foreach:
  815.       {
  816.     /* Get three comma-separated arguments but
  817.        expand only the first two.  */
  818.     char *var, *list;
  819.     register struct variable *v;
  820.  
  821.     count = 0;
  822.     for (p = text; p < end; ++p)
  823.       {
  824.         if (*p == startparen)
  825.           ++count;
  826.         else if (*p == endparen)
  827.           --count;
  828.         else if (*p == ',' && count <= 0)
  829.           break;
  830.       }
  831.     if (p == end)
  832.       BADARGS ("foreach");
  833.     var = expand_argument (text, p);
  834.  
  835.     p2 = p + 1;
  836.     count = 0;
  837.     for (p = p2; p < end; ++p)
  838.       {
  839.         if (*p == startparen)
  840.           ++count;
  841.         else if (*p == endparen)
  842.           --count;
  843.         else if (*p == ',' && count <= 0)
  844.           break;
  845.       }
  846.     if (p == end)
  847.       BADARGS ("foreach");
  848.     list = expand_argument (p2, p);
  849.  
  850.     ++p;
  851.     text = savestring (p, end - p);
  852.  
  853.     push_new_variable_scope ();
  854.     v = define_variable (var, strlen (var), "", o_automatic, 0);
  855.     p3 = list;
  856.     while ((p = find_next_token (&p3, &len)) != 0)
  857.       {
  858.         char *result;
  859.         char save = p[len];
  860.         p[len] = '\0';
  861.         v->value = p;
  862.         result = allocated_variable_expand (text);
  863.         p[len] = save;
  864.  
  865.         o = variable_buffer_output (o, result, strlen (result));
  866.         o = variable_buffer_output (o, " ", 1);
  867.         doneany = 1;
  868.         free (result);
  869.       }
  870.     if (doneany)
  871.       /* Kill the last space.  */
  872.       --o;
  873.  
  874.     pop_variable_scope ();
  875.  
  876.     free (var);
  877.     free (list);
  878.     free (text);
  879.       }
  880.       break;
  881.  
  882.     case function_filter:
  883.     case function_filter_out:
  884.       {
  885.     struct word
  886.       {
  887.         struct word *next;
  888.         char *word;
  889.         int matched;
  890.       } *words, *wordtail, *wp;
  891.  
  892.     /* Get two comma-separated arguments and expand each one.  */
  893.     count = 0;
  894.     for (p = text; p < end; ++p)
  895.       {
  896.         if (*p == startparen)
  897.           ++count;
  898.         else if (*p == endparen)
  899.           --count;
  900.         else if (*p == ',' && count <= 0)
  901.           break;
  902.       }
  903.     if (p == end)
  904.       BADARGS (function == function_filter ? "filter" : "filter-out");
  905.     p2 = expand_argument (text, p);
  906.  
  907.     text = expand_argument (p + 1, end);
  908.  
  909.     /* Chop TEXT up into words and then run each pattern through.  */
  910.     words = wordtail = 0;
  911.     p3 = text;
  912.     while ((p = find_next_token (&p3, &len)) != 0)
  913.       {
  914.         struct word *w = (struct word *) alloca (sizeof (struct word));
  915.         if (words == 0)
  916.           words = w;
  917.         else
  918.           wordtail->next = w;
  919.         wordtail = w;
  920.  
  921.         if (*p3 != '\0')
  922.           ++p3;
  923.         p[len] = '\0';
  924.         w->word = p;
  925.         w->matched = 0;
  926.       }
  927.  
  928.     if (words != 0)
  929.       {
  930.         wordtail->next = 0;
  931.  
  932.         /* Run each pattern through the words, killing words.  */
  933.         p3 = p2;
  934.         while ((p = find_next_token (&p3, &len)) != 0)
  935.           {
  936.         char *percent;
  937.         char save = p[len];
  938.         p[len] = '\0';
  939.  
  940.         percent = find_percent (p);
  941.         for (wp = words; wp != 0; wp = wp->next)
  942.           wp->matched |= (percent == 0 ? streq (p, wp->word)
  943.                   : pattern_matches (p, percent, wp->word));
  944.  
  945.         p[len] = save;
  946.           }
  947.  
  948.         /* Output the words that matched (or didn't, for filter-out).  */
  949.         for (wp = words; wp != 0; wp = wp->next)
  950.           if (function == function_filter ? wp->matched : !wp->matched)
  951.         {
  952.           o = variable_buffer_output (o, wp->word, strlen (wp->word));
  953.           o = variable_buffer_output (o, " ", 1);
  954.           doneany = 1;
  955.         }
  956.         if (doneany)
  957.           /* Kill the last space.  */
  958.           --o;
  959.       }
  960.  
  961.     free (p2);
  962.     free (text);
  963.       }
  964.       break;
  965.  
  966.     case function_patsubst:
  967.       /* Get three comma-separated arguments and expand each one.  */
  968.       count = 0;
  969.       for (p = text; p < end; ++p)
  970.     {
  971.       if (*p == startparen)
  972.         ++count;
  973.       else if (*p == endparen)
  974.         --count;
  975.       else if (*p == ',' && count <= 0)
  976.         break;
  977.     }
  978.       if (p == end)
  979.     BADARGS ("patsubst");
  980.  
  981.       p2 = p;
  982.       count = 0;
  983.       for (++p; p < end; ++p)
  984.     {
  985.       if (*p == startparen)
  986.         ++count;
  987.       else if (*p == endparen)
  988.         --count;
  989.       else if (*p == ',' && count <= 0)
  990.         break;
  991.     }
  992.       if (p == end)
  993.     BADARGS ("patsubst");
  994.  
  995.       text = expand_argument (text, p2);
  996.       p3 = expand_argument (p2 + 1, p);
  997.       p2 = expand_argument (p + 1, end);
  998.  
  999.       o = patsubst_expand (o, p2, text, p3, (char *) 0, (char *) 0);
  1000.  
  1001.       free (text);
  1002.       free (p3);
  1003.       free (p2);
  1004.       break;
  1005.  
  1006.     case function_join:
  1007.       /* Get two comma-separated arguments and expand each one.  */
  1008.       count = 0;
  1009.       for (p = text; p < end; ++p)
  1010.     {
  1011.       if (*p == startparen)
  1012.         ++count;
  1013.       else if (*p == endparen)
  1014.         --count;
  1015.       else if (*p == ',' && count <= 0)
  1016.         break;
  1017.     }
  1018.       if (p == end)
  1019.     BADARGS ("join");
  1020.       text = expand_argument (text, p);
  1021.  
  1022.       p = expand_argument (p + 1, end);
  1023.  
  1024.       {
  1025.     /* Write each word of the first argument directly followed
  1026.        by the corresponding word of the second argument.
  1027.        If the two arguments have a different number of words,
  1028.        the excess words are just output separated by blanks.  */
  1029.     register char *tp, *pp;
  1030.     p2 = text;
  1031.     p3 = p;
  1032.     do
  1033.       {
  1034.         unsigned int tlen, plen;
  1035.  
  1036.         tp = find_next_token (&p2, &tlen);
  1037.         if (tp != 0)
  1038.           o = variable_buffer_output (o, tp, tlen);
  1039.  
  1040.         pp = find_next_token (&p3, &plen);
  1041.         if (pp != 0)
  1042.           o = variable_buffer_output (o, pp, plen);
  1043.  
  1044.         if (tp != 0 || pp != 0)
  1045.           {
  1046.         o = variable_buffer_output (o, " ", 1);
  1047.         doneany = 1;
  1048.           }
  1049.       }
  1050.     while (tp != 0 || pp != 0);
  1051.     if (doneany)
  1052.       /* Kill the last blank.  */
  1053.       --o;
  1054.       }
  1055.  
  1056.       free (text);
  1057.       free (p);
  1058.       break;
  1059.  
  1060.     case function_strip:
  1061.       /* Expand the argument.  */
  1062.       text = expand_argument (text, end);
  1063.  
  1064.       p2 = text;
  1065.       while (*p2 != '\0')
  1066.     {
  1067.       while (isspace(*p2))
  1068.         ++p2;
  1069.       p = p2;
  1070.       for (i=0; *p2 != '\0' && !isspace(*p2); ++p2, ++i)
  1071.         {}
  1072.       if (!i)
  1073.         break;
  1074.       o = variable_buffer_output (o, p, i);
  1075.       o = variable_buffer_output (o, " ", 1);
  1076.       doneany = 1;
  1077.     }
  1078.       if (doneany)
  1079.     /* Kill the last space.  */
  1080.     --o;
  1081.  
  1082.       free (text);
  1083.       break;
  1084.  
  1085.     case function_wildcard:
  1086.       text = expand_argument (text, end);
  1087.  
  1088. #ifdef _AMIGA
  1089.       o = wildcard_expansion (text, o);
  1090. #else
  1091.       p = string_glob (text);
  1092.       o = variable_buffer_output (o, p, strlen (p));
  1093. #endif
  1094.  
  1095.       free (text);
  1096.       break;
  1097.  
  1098.     case function_subst:
  1099.       /* Get three comma-separated arguments and expand each one.  */
  1100.       count = 0;
  1101.       for (p = text; p < end; ++p)
  1102.     {
  1103.       if (*p == startparen)
  1104.         ++count;
  1105.       else if (*p == endparen)
  1106.         --count;
  1107.       else if (*p == ',' && count <= 0)
  1108.         break;
  1109.     }
  1110.       if (p == end)
  1111.     BADARGS ("subst");
  1112.  
  1113.       p2 = p;
  1114.       count = 0;
  1115.       for (++p; p < end; ++p)
  1116.     {
  1117.       if (*p == startparen)
  1118.         ++count;
  1119.       else if (*p == endparen)
  1120.         --count;
  1121.       else if (*p == ',' && count <= 0)
  1122.         break;
  1123.     }
  1124.       if (p == end)
  1125.     BADARGS ("subst");
  1126.  
  1127.       text = expand_argument (text, p2);
  1128.       p3 = expand_argument (p2 + 1, p);
  1129.       p2 = expand_argument (p + 1, end);
  1130.  
  1131.       o = subst_expand (o, p2, text, p3, strlen (text), strlen (p3), 0, 0);
  1132.  
  1133.       free (text);
  1134.       free (p3);
  1135.       free (p2);
  1136.       break;
  1137.  
  1138.     case function_firstword:
  1139.       /* Expand the argument.  */
  1140.       text = expand_argument (text, end);
  1141.  
  1142.       /* Find the first word in TEXT.  */
  1143.       p2 = text;
  1144.       p = find_next_token (&p2, &i);
  1145.       if (p != 0)
  1146.     o = variable_buffer_output (o, p, i);
  1147.  
  1148.       free (text);
  1149.       break;
  1150.  
  1151.     case function_word:
  1152.       /* Get two comma-separated arguments and expand each one.  */
  1153.       count = 0;
  1154.       for (p = text; p < end; ++p)
  1155.     {
  1156.       if (*p == startparen)
  1157.         ++count;
  1158.       else if (*p == endparen)
  1159.         --count;
  1160.       else if (*p == ',' && count <= 0)
  1161.         break;
  1162.     }
  1163.       if (p == end)
  1164.     BADARGS ("word");
  1165.       text = expand_argument (text, p);
  1166.  
  1167.       p3 = expand_argument (p + 1, end);
  1168.  
  1169.       /* Check the first argument.  */
  1170.       for (p2 = text; *p2 != '\0'; ++p2)
  1171.     if (*p2 < '0' || *p2 > '9')
  1172.       {
  1173.         if (reading_filename != 0)
  1174.           makefile_fatal (reading_filename, *reading_lineno_ptr,
  1175.                   "non-numeric first argument to `word' function");
  1176.         else
  1177.           fatal ("non-numeric first argument to `word' function");
  1178.       }
  1179.  
  1180.       i = (unsigned int) atoi (text);
  1181.       if (i == 0)
  1182.     {
  1183.       if (reading_filename != 0)
  1184.         makefile_fatal (reading_filename, *reading_lineno_ptr,
  1185.                 "the `word' function takes a one-origin \
  1186. index argument");
  1187.       else
  1188.         fatal ("the `word' function takes a one-origin index argument");
  1189.     }
  1190.  
  1191.       p2 = p3;
  1192.       while ((p = find_next_token (&p2, &len)) != 0)
  1193.     if (--i == 0)
  1194.       break;
  1195.       if (i == 0)
  1196.     o = variable_buffer_output (o, p, len);
  1197.  
  1198.       free (text);
  1199.       free (p3);
  1200.       break;
  1201.  
  1202.     case function_words:
  1203.       /* Expand the argument.  */
  1204.       text = expand_argument (text, end);
  1205.  
  1206.       i = 0;
  1207.       p2 = text;
  1208.       while (find_next_token (&p2, (unsigned int *) 0) != 0)
  1209.     ++i;
  1210.  
  1211.       {
  1212.     char buf[20];
  1213.     sprintf (buf, "%d", i);
  1214.     o = variable_buffer_output (o, buf, strlen (buf));
  1215.       }
  1216.  
  1217.       free (text);
  1218.       break;
  1219.  
  1220.     case function_wordlist:
  1221.       /* Get two comma-separated arguments and expand each one.  */
  1222.       count = 0;
  1223.       for (p = text; p < end; ++p)
  1224.     {
  1225.       if (*p == startparen)
  1226.         ++count;
  1227.       else if (*p == endparen)
  1228.         --count;
  1229.       else if (*p == ',' && count <= 0)
  1230.         break;
  1231.     }
  1232.       if (p == end)
  1233.     BADARGS ("wordlist");
  1234.       text = expand_argument (text, p);
  1235.  
  1236.       /* Check the first argument.  */
  1237.       for (p2 = text; *p2 != '\0'; ++p2)
  1238.     if (*p2 < '0' || *p2 > '9')
  1239.       {
  1240.         if (reading_filename != 0)
  1241.           makefile_fatal (reading_filename, *reading_lineno_ptr,
  1242.                   "non-numeric first argument to `wordlist' function");
  1243.         else
  1244.           fatal ("non-numeric first argument to `wordlist' function");
  1245.       }
  1246.       i = (unsigned int)atoi(text);
  1247.       free (text);
  1248.  
  1249.       /* Check the next argument */
  1250.       for (p2 = p + 1; isblank(*p2); ++p2)
  1251.     {}
  1252.       count = 0;
  1253.       for (p = p2; p < end; ++p)
  1254.     {
  1255.       if (*p == startparen)
  1256.         ++count;
  1257.       else if (*p == endparen)
  1258.         --count;
  1259.       else if (*p == ',' && count <= 0)
  1260.         break;
  1261.     }
  1262.       if (p == end)
  1263.     BADARGS ("wordlist");
  1264.       text = expand_argument (p2, p);
  1265.  
  1266.       for (p2 = text; *p2 != '\0'; ++p2)
  1267.     if (*p2 < '0' || *p2 > '9')
  1268.       {
  1269.         if (reading_filename != 0)
  1270.           makefile_fatal (reading_filename, *reading_lineno_ptr,
  1271.                   "non-numeric second argument to `wordlist' function");
  1272.         else
  1273.           fatal ("non-numeric second argument to `wordlist' function");
  1274.       }
  1275.       j = (unsigned int)atoi(text);
  1276.       free (text);
  1277.  
  1278.       if (j > i)
  1279.     j -= i;
  1280.       else
  1281.     {
  1282.       unsigned int k;
  1283.       k = j;
  1284.       j = i - j;
  1285.       i = k;
  1286.     }
  1287.       ++j;
  1288.  
  1289.       /* Extract the requested words */
  1290.       text = expand_argument (p + 1, end);
  1291.       p2 = text;
  1292.  
  1293.       while (((p = find_next_token (&p2, &len)) != 0) && --i)
  1294.     {}
  1295.       if (p)
  1296.     {
  1297.       while (--j && (find_next_token (&p2, &len) != 0))
  1298.         {}
  1299.       o = variable_buffer_output (o, p, p2 - p);
  1300.     }
  1301.  
  1302.       free (text);
  1303.       break;
  1304.  
  1305.     case function_findstring:
  1306.       /* Get two comma-separated arguments and expand each one.  */
  1307.       count = 0;
  1308.       for (p = text; p < end; ++p)
  1309.     {
  1310.       if (*p == startparen)
  1311.         ++count;
  1312.       else if (*p == endparen)
  1313.         --count;
  1314.       else if (*p == ',' && count <= 0)
  1315.         break;
  1316.     }
  1317.       if (p == end)
  1318.     BADARGS ("findstring");
  1319.       text = expand_argument (text, p);
  1320.  
  1321.       p = expand_argument (p + 1, end);
  1322.  
  1323.       /* Find the first occurrence of the first string in the second.  */
  1324.       i = strlen (text);
  1325.       if (sindex (p, 0, text, i) != 0)
  1326.     o = variable_buffer_output (o, text, i);
  1327.  
  1328.       free (p);
  1329.       free (text);
  1330.       break;
  1331.  
  1332.     case function_addsuffix:
  1333.     case function_addprefix:
  1334.       /* Get two comma-separated arguments and expand each one.  */
  1335.       count = 0;
  1336.       for (p = text; p < end; ++p)
  1337.     {
  1338.       if (*p == startparen)
  1339.         ++count;
  1340.       else if (*p == endparen)
  1341.         --count;
  1342.       else if (*p == ',' && count <= 0)
  1343.         break;
  1344.     }
  1345.       if (p == end)
  1346.     BADARGS (function == function_addsuffix ? "addsuffix" : "addprefix");
  1347.       text = expand_argument (text, p);
  1348.       i = strlen (text);
  1349.  
  1350.       p2 = expand_argument (p + 1, end);
  1351.  
  1352.       p3 = p2;
  1353.       while ((p = find_next_token (&p3, &len)) != 0)
  1354.     {
  1355.       if (function == function_addprefix)
  1356.         o = variable_buffer_output (o, text, i);
  1357.       o = variable_buffer_output (o, p, len);
  1358.       if (function == function_addsuffix)
  1359.         o = variable_buffer_output (o, text, i);
  1360.       o = variable_buffer_output (o, " ", 1);
  1361.       doneany = 1;
  1362.     }
  1363.       if (doneany)
  1364.     /* Kill last space.  */
  1365.     --o;
  1366.  
  1367.       free (p2);
  1368.       free (text);
  1369.       break;
  1370.  
  1371.     case function_dir:
  1372.     case function_basename:
  1373.       /* Expand the argument.  */
  1374.       text = expand_argument (text, end);
  1375.  
  1376.       p3 = text;
  1377.       while ((p2 = find_next_token (&p3, &len)) != 0)
  1378.     {
  1379.       p = p2 + len;
  1380. #ifdef VMS
  1381.       while (p >= p2 && *p != ']'
  1382.          && (function != function_basename || *p != '.'))
  1383. #else
  1384. # ifdef __MSDOS__
  1385.       while (p >= p2 && *p != '/' && *p != '\\'
  1386.          && (function != function_basename || *p != '.'))
  1387. # else
  1388.       while (p >= p2 && *p != '/'
  1389.          && (function != function_basename || *p != '.'))
  1390. # endif
  1391. #endif
  1392.         --p;
  1393.       if (p >= p2 && (function == function_dir))
  1394.         o = variable_buffer_output (o, p2, ++p - p2);
  1395.       else if (p >= p2 && (*p == '.'))
  1396.         o = variable_buffer_output (o, p2, p - p2);
  1397. #if defined(WINDOWS32) || defined(__MSDOS__)
  1398.     /* Handle the "d:foobar" case */
  1399.       else if (p2[0] && p2[1] == ':' && function == function_dir)
  1400.         o = variable_buffer_output (o, p2, 2);
  1401. #endif
  1402.       else if (function == function_dir)
  1403. #ifdef VMS
  1404.         o = variable_buffer_output (o, "[]", 2);
  1405. #else
  1406. #ifndef _AMIGA
  1407.         o = variable_buffer_output (o, "./", 2);
  1408. #else
  1409.         /* o = o */; /* Just a nop...  */
  1410. #endif /* AMIGA */
  1411. #endif /* !VMS */
  1412.       else
  1413.         /* The entire name is the basename.  */
  1414.         o = variable_buffer_output (o, p2, len);
  1415.  
  1416.       o = variable_buffer_output (o, " ", 1);
  1417.       doneany = 1;
  1418.     }
  1419.       if (doneany)
  1420.     /* Kill last space.  */
  1421.     --o;
  1422.  
  1423.       free (text);
  1424.       break;
  1425.  
  1426.     case function_notdir:
  1427.     case function_suffix:
  1428.       /* Expand the argument.  */
  1429.       text = expand_argument (text, end);
  1430.  
  1431.       p3 = text;
  1432.       while ((p2 = find_next_token (&p3, &len)) != 0)
  1433.     {
  1434.       p = p2 + len;
  1435. #ifdef VMS
  1436.       while (p >= p2 && *p != ']'
  1437.          && (function != function_suffix || *p != '.'))
  1438. #else
  1439. # ifdef __MSDOS__
  1440.       while (p >= p2 && *p != '/' && *p != '\\'
  1441.          && (function != function_suffix || *p != '.'))
  1442. # else
  1443.       while (p >= p2 && *p != '/'
  1444.          && (function != function_suffix || *p != '.'))
  1445. # endif
  1446. #endif
  1447.         --p;
  1448.       if (p >= p2)
  1449.         {
  1450.           if (function == function_notdir)
  1451.         ++p;
  1452.           else if (*p != '.')
  1453.         continue;
  1454.           o = variable_buffer_output (o, p, len - (p - p2));
  1455.         }
  1456. #if defined(WINDOWS32) || defined(__MSDOS__)
  1457.       /* Handle the case of "d:foo/bar".  */
  1458.       else if (function == function_notdir && p2[0] && p2[1] == ':')
  1459.         {
  1460.           p = p2 + 2;
  1461.           o = variable_buffer_output (o, p, len - (p - p2));
  1462.         }
  1463. #endif
  1464.       else if (function == function_notdir)
  1465.         o = variable_buffer_output (o, p2, len);
  1466.  
  1467.       if (function == function_notdir || p >= p2)
  1468.         {
  1469.           o = variable_buffer_output (o, " ", 1);
  1470.           doneany = 1;
  1471.         }
  1472.     }
  1473.       if (doneany)
  1474.     /* Kill last space.  */
  1475.     --o;
  1476.  
  1477.       free (text);
  1478.       break;
  1479.     }
  1480.  
  1481.   return o;
  1482. }
  1483.  
  1484. /* Check for a function invocation in *STRINGP.  *STRINGP points at the
  1485.    opening ( or { and is not null-terminated.  If a function invocation
  1486.    is found, expand it into the buffer at *OP, updating *OP, incrementing
  1487.    *STRINGP past the reference and returning nonzero.  If not, return zero.  */
  1488.  
  1489. int
  1490. handle_function (op, stringp)
  1491.      char **op;
  1492.      char **stringp;
  1493.  
  1494. {
  1495.   register unsigned int code;
  1496.   unsigned int maxlen;
  1497.   char *beg = *stringp + 1;
  1498.   char *endref;
  1499.  
  1500.   endref = lindex (beg, beg + MAXFUNCTIONLEN, '\0');
  1501.   maxlen = endref != 0 ? endref - beg : MAXFUNCTIONLEN;
  1502.  
  1503.   for (code = 0; function_table[code].name != 0; ++code)
  1504.     {
  1505.       if (maxlen < function_table[code].len)
  1506.     continue;
  1507.       endref = beg + function_table[code].len;
  1508.       if (isblank (*endref)
  1509.       && !strncmp (function_table[code].name, beg,
  1510.                function_table[code].len))
  1511.     break;
  1512.     }
  1513.   if (function_table[code].name != 0)
  1514.     {
  1515.       /* We have found a call to an expansion-time function.
  1516.      Find the end of the arguments, and do the function.  */
  1517.  
  1518.       char openparen = beg[-1], closeparen = openparen == '(' ? ')' : '}';
  1519.       int count = 0;
  1520.       char *argbeg;
  1521.       register char *p;
  1522.  
  1523.       /* Space after function name isn't part of the args.  */
  1524.       p = next_token (endref);
  1525.       argbeg = p;
  1526.  
  1527.       /* Count nested use of whichever kind of parens we use,
  1528.      so that nested calls and variable refs work.  */
  1529.  
  1530.       for (; *p != '\0'; ++p)
  1531.     {
  1532.       if (*p == openparen)
  1533.         ++count;
  1534.       else if (*p == closeparen && --count < 0)
  1535.         break;
  1536.     }
  1537.  
  1538.       if (count >= 0)
  1539.     {
  1540.       static const char errmsg[]
  1541.         = "unterminated call to function `%s': missing `%c'";
  1542.       if (reading_filename == 0)
  1543.         fatal (errmsg, function_table[code].name, closeparen);
  1544.       else
  1545.         makefile_fatal (reading_filename, *reading_lineno_ptr, errmsg,
  1546.                 function_table[code].name, closeparen);
  1547.     }
  1548.  
  1549.       /* We found the end; expand the function call.  */
  1550.  
  1551.       *op = expand_function (*op, function_table[code].function, argbeg, p);
  1552.       *stringp = p;
  1553.       return 1;
  1554.     }
  1555.  
  1556.   return 0;
  1557. }
  1558.  
  1559. /* Glob-expand LINE.  The returned pointer is
  1560.    only good until the next call to string_glob.  */
  1561.  
  1562. static char *
  1563. string_glob (line)
  1564.      char *line;
  1565. {
  1566.   static char *result = 0;
  1567.   static unsigned int length;
  1568.   register struct nameseq *chain;
  1569.   register unsigned int idx;
  1570.  
  1571.   chain = multi_glob (parse_file_seq
  1572.               (&line, '\0', sizeof (struct nameseq),
  1573.                /* We do not want parse_file_seq to strip `./'s.
  1574.               That would break examples like:
  1575.               $(patsubst ./%.c,obj/%.o,$(wildcard ./*.c)).  */
  1576.                0),
  1577.               sizeof (struct nameseq));
  1578.  
  1579.   if (result == 0)
  1580.     {
  1581.       length = 100;
  1582.       result = (char *) xmalloc (100);
  1583.     }
  1584.  
  1585.   idx = 0;
  1586.   while (chain != 0)
  1587.     {
  1588.       register char *name = chain->name;
  1589.       unsigned int len = strlen (name);
  1590.  
  1591.       struct nameseq *next = chain->next;
  1592.       free ((char *) chain);
  1593.       chain = next;
  1594.  
  1595.       /* multi_glob will pass names without globbing metacharacters
  1596.      through as is, but we want only files that actually exist.  */
  1597.       if (file_exists_p (name))
  1598.     {
  1599.       if (idx + len + 1 > length)
  1600.         {
  1601.           length += (len + 1) * 2;
  1602.           result = (char *) xrealloc (result, length);
  1603.         }
  1604.       bcopy (name, &result[idx], len);
  1605.       idx += len;
  1606.       result[idx++] = ' ';
  1607.     }
  1608.  
  1609.       free (name);
  1610.     }
  1611.  
  1612.   /* Kill the last space and terminate the string.  */
  1613.   if (idx == 0)
  1614.     result[0] = '\0';
  1615.   else
  1616.     result[idx - 1] = '\0';
  1617.  
  1618.   return result;
  1619. }
  1620.